#!/bin/sh

# 110808 WH increase psCNT to allow script to run
# 110816 WH Update to sort files into correct directories

icommandsDir="/opt/iRODS/iRODS/clients/icommands/bin"
today=`date '+%Y-%m-%d'`
curTime=`date '+%Y-%m-%d-%H-%M-%S'`
echo $curTime 

#psCNT=`ps -ef|grep ACORN|wc -l`
#if [ $psCNT -gt 0 ]; then
#   echo "exit due to more than 1 ACORN_Data_Move1seastateonly.sh running at this moment"
#   exit 0
#fi
if [ -f /var/tmp/ACORN_Data_Move1seastateonly.pid ]; then
   echo "exit due to seastateonly.sh already running"
   #ps -ef|grep `cat /var/tmp/ACORN_Data_Move1seastateonly.pid`
   exit 0
else
   echo "$$" > /var/tmp/ACORN_Data_Move1seastateonly.pid
fi


resc_status=`$icommandsDir/iadmin lr arcs-df.qcif.edu.au|grep auto|cut -d' ' -f2`
if [[ $resc_status == *down* ]]; then
   echo "exit due to unavailable resource arcs-df.qcif.edu.au"
   exit 0
fi

# $icommandsDir/icp -rf /ARCS/projects/IMOS/staging/ACORN/range-time-series /ARCS/projects/IMOS/archive/ACORN
# $icommandsDir/icp -rf /ARCS/projects/IMOS/staging/ACORN/calibration-time-series /ARCS/projects/IMOS/archive/ACORN
echo "complete copying to archive" 

# $icommandsDir/icp -rf /ARCS/projects/IMOS/staging/ACORN/radial /ARCS/projects/IMOS/opendap/ACORN
# echo "complete copying to radial" 

# $icommandsDir/icp -rf /ARCS/projects/IMOS/staging/ACORN/sea-state /ARCS/projects/IMOS/opendap/ACORN
# echo "complete copying to sea-state" 

find /data/Vault/ARCS/projects/IMOS/opendap/ACORN/gridded_1h-avg-current-map_non-QC  -type d ! \( -perm -g=x \) -exec chmod -R g+xr {} \;
find /data/Vault/ARCS/projects/IMOS/opendap/ACORN/gridded_1h-avg-current-map_non-QC  -type f ! \( -perm -g=r \) -exec chmod g+r {} \;  -exec chgrp jetty {} \;
echo "complete changing permissions to gridded_1h-avg-current-map_non-QC"

find /data/Vault/ARCS/projects/IMOS/opendap/ACORN/gridded_1h-avg-current-map_QC  -type d ! \( -perm -g=x \) -exec chmod -R g+xr {} \;
find /data/Vault/ARCS/projects/IMOS/opendap/ACORN/gridded_1h-avg-current-map_QC  -type f ! \( -perm -g=r \) -exec chmod g+r {} \;  -exec chgrp jetty {} \;
echo "complete changing permissions to gridded_1h-avg-current-map_QC"

find /data/Vault/ARCS/projects/IMOS/opendap/FAIMMS  -type d ! \( -perm -g=x \) -exec chmod -R g+xr {} \;
find /data/Vault/ARCS/projects/IMOS/opendap/FAIMMS  -type f ! \( -perm -g=r \) -exec chmod g+r {} \;  -exec chgrp jetty {} \;
echo "complete changing permissions to FAIMMS"


for irodsFileName in `find /data/Vault/ARCS/projects/IMOS/staging/ACORN -type f -mmin +5 |cut -c12-|grep state.nc`
do
   # FV00_range-time-series.nc    -------->  archive\ACORN\range-time-series
   if [[ $irodsFileName == *range-time-series* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/archive/g'|sed 's/ACORN\/radial/ACORN\/range-time-series/g'`
   # FV00_calibration-time-series -------->  archive\ACORN\calibration-time-series	
   elif [[ $irodsFileName == *calibration-time-series* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/archive/g'|sed 's/ACORN\/radial/ACORN\/calibration-time-series/g'`
   # FV00_rf-scan
   elif [[ $irodsFileName == *rf-scan* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/archive/g'|sed 's/ACORN\/radial/ACORN\/rf-scan/g'`
   # FV00_sea-state.nc  ---------> opendap\ACORN\sea-state
   elif [[ $irodsFileName == *sea-state* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/opendap/g'`
   # FV00_radial -------->  opendap\ACORN\radial
   elif [[ $irodsFileName == *FV00_radial* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/opendap/g'`
   # FV01_radial -------->  opendap\ACORN\radial_quality_controlled
   elif [[ $irodsFileName == *FV01_radial* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/opendap/g'|sed 's/ACORN\/radial/ACORN\/radial_quality_controlled/g'`
   else
       echo "unknown file encountered - $irodsFileName"
       exit 2
   fi
        opendap_PATH=${fileCopy%\/*}
        COLL=`$icommandsDir/iquest "SELECT COLL_NAME WHERE COLL_NAME like '$opendap_PATH%'" 2>/dev/null`
        COLL=${COLL##} 
        COLL=${COLL%%}
        COLL_LEN=${#COLL}
        if [ $COLL_LEN -ne 0 ]; then  
               if [ -n "`ls /data/Vault$fileCopy 2>/dev/null`" ]; then
                    echo '###100' $irodsFileName
                    if ! $icommandsDir/icp -f $irodsFileName $opendap_PATH; then
                         $icommandsDir/irm -f $fileCopy
                    fi
               else
                    echo '###101' $irodsFileName
                    $icommandsDir/imv $irodsFileName $opendap_PATH
               fi      
        else
               echo '###1' $irodsFileName
               $icommandsDir/imkdir -p $opendap_PATH
               $icommandsDir/imv $irodsFileName $opendap_PATH
               n=0
               PARSED_PATH=$opendap_PATH
               while [ $n -lt 4 ]
               do
                   chmod -R g+xr /data/Vault$PARSED_PATH
                   PARSED_PATH=${PARSED_PATH%\/*}
                   n=`expr $n + 1`
               done
        fi
        chmod g+r /data/Vault$fileCopy
        chgrp jetty /data/Vault$fileCopy
done
echo "complete copying to sea-state"

for irodsFileName in `find /data/Vault/ARCS/projects/IMOS/staging/ACORN -type f -mmin +60|cut -c12-|grep state.nc`
do
   # FV00_range-time-series.nc    -------->  archive\ACORN\range-time-series
   if [[ $irodsFileName == *range-time-series* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/archive/g'|sed 's/ACORN\/radial/ACORN\/range-time-series/g'`
   # FV00_calibration-time-series -------->  archive\ACORN\calibration-time-series	
   elif [[ $irodsFileName == *calibration-time-series* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/archive/g'|sed 's/ACORN\/radial/ACORN\/calibration-time-series/g'`
   # FV00_rf-scan
   elif [[ $irodsFileName == *rf-scan* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/archive/g'|sed 's/ACORN\/radial/ACORN\/rf-scan/g'`
   # FV00_sea-state.nc  ---------> opendap\ACORN\sea-state
   elif [[ $irodsFileName == *sea-state* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/opendap/g'`
   # FV00_radial -------->  opendap\ACORN\radial
   elif [[ $irodsFileName == *FV00_radial* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/opendap/g'`
   # FV01_radial -------->  opendap\ACORN\radial_quality_controlled
   elif [[ $irodsFileName == *FV01_radial* ]]; then
   	fileCopy=`echo $irodsFileName|sed 's/staging/opendap/g'|sed 's/ACORN\/radial/ACORN\/radial_quality_controlled/g'`
   else
       echo "unknown file encountered - $irodsFileName"
       exit 2
   fi
   if [ -n "`ls /data/Vault$fileCopy 2>/dev/null`" ]; then
         fileName=`basename $fileCopy`
         if [ -z `$icommandsDir/iquest "SELECT RESC_NAME, RESC_STATUS  WHERE DATA_NAME = '$fileName'"|grep down` ]; then
              echo "deleted file:" $irodsFileName `stat -c %y "/data/Vault"$irodsFileName`
              $icommandsDir/irm -f $irodsFileName        
         else
              echo "exit" $irodsFileName `stat -c %y "/data/Vault"$irodsFileName`
         fi
   fi
done
echo "complete deletion" 

curTime1=`date '+%Y-%m-%d-%H-%M-%S'`
echo "$curTime1 complete"
rm /var/tmp/ACORN_Data_Move1seastateonly.pid

